package com.yancy.service.impl;

import com.yancy.dao.UserDao;
import com.yancy.dao.impl.UserDaoImpl;
import com.yancy.entity.User;
import com.yancy.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    public boolean userLogin(User user){
        UserDao userDao = new UserDaoImpl();
//        dao层返回的对象 可能为空 或者密码不正确
        User queryUser = userDao.userLogin(user.getUserName());
        if(user.getPassWord().equals(queryUser.getPassWord())){
            return true;
        }
        return false;
    }

    @Override
    public List<User> getUser() {
        UserDao userDao = new UserDaoImpl();
        return userDao.getUser();
    }

    @Override
    public boolean deleteUser(String id) {
        UserDao userDao = new UserDaoImpl();
        return userDao.deleteuser(id);
    }

    @Override
    public boolean addUser(HttpServletRequest req) {
        User user = new User(req.getParameter("userName"),req.getParameter("passWord"));
        String uuid = UUID.randomUUID().toString().replaceAll("-","").substring(0,10);
        user.setId(uuid);
        UserDao userDao = new UserDaoImpl();
        return userDao.addUser(user);
    }

    @Override
    public boolean updateUser(HttpServletRequest req) {
        User user = new User(req.getParameter("id"),req.getParameter("userName"),req.getParameter("passWord"));
        UserDao userDao = new UserDaoImpl();
        return userDao.updateUser(user);
    }
}
